<template>
    <div>
        <el-row :gutter="15">
			<el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="130px" label-position="right" :disabled="setting.readonly">
				<el-col :span="12" v-if="JudgeShow('flowNumber')">
					<el-form-item label="流程编号" prop="flowNumber" >
						<el-input v-model="dataForm.flowNumber"  :disabled ="JudgeWrite('flowNumber')"  placeholder="系统自动生成" readonly >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('sgdj')">
					<el-form-item label="事故等级" prop="sgdj" required >
						<el-select v-model="dataForm.sgdj"  :disabled ="JudgeWrite('sgdj')"  placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in sgdjOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('sgfxx')">
					<el-form-item label="事故发现项" prop="sgfxx" required >
						<el-input v-model="dataForm.sgfxx"  :disabled ="JudgeWrite('sgfxx')"  placeholder="请输入" clearable :style='{"width":"100%"}' >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('zgzrr')">
					<el-form-item label="整改责任人" prop="zgzrr" required >
						<el-select v-model="dataForm.zgzrr"  :disabled ="JudgeWrite('zgzrr')"  placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in zgzrrOptions" :key="index" :label="item.F_RealName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('spr')">
					<el-form-item label="审批人" prop="spr" required >
						<el-select v-model="dataForm.spr"  :disabled ="JudgeWrite('spr')"  placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in sprOptions" :key="index" :label="item.F_RealName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('jywcqx')">
					<el-form-item label="建议完成期限" prop="jywcqx" required >
						<el-date-picker v-model="dataForm.jywcqx"  :disabled ="JudgeWrite('jywcqx')"  placeholder="请选择" clearable :style='{"width":"100%"}' type="date" format="yyyy-MM-dd" value-format="timestamp" >
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('sgzjl')">
					<el-form-item label="事故证据链" prop="sgzjl" >
						<JNPF-UploadFz v-model="sgzjlList"  :disabled ="JudgeWrite('sgzjl')"  accept="" :fileSize="10" sizeUnit="MB" :limit="1" buttonText="点击上传" >
						</JNPF-UploadFz>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('sgsfxb')">
					<el-form-item label="事故树分析表" prop="sgsfxb" >
						<JNPF-UploadFz v-model="sgsfxbList"  :disabled ="JudgeWrite('sgsfxb')"  accept="" :fileSize="10" sizeUnit="MB" :limit="1" buttonText="点击上传" >
						</JNPF-UploadFz>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('zzdcbg')">
					<el-form-item label="最终调查报告" prop="zzdcbg" >
						<JNPF-UploadFz v-model="zzdcbgList"  :disabled ="JudgeWrite('zzdcbg')"  accept="" :fileSize="10" sizeUnit="MB" :limit="1" buttonText="点击上传" >
						</JNPF-UploadFz>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('mainid')">
					<el-form-item label="主表ID" prop="mainid" >
						<el-input v-model="dataForm.mainid" disabled placeholder="请输入" clearable :style='{"width":"100%"}' >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('hzssyx')">
					<el-form-item label="火灾损失影响" prop="hzssyx" required >
						<el-input v-model="dataForm.hzssyx"  :disabled ="JudgeWrite('hzssyx')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('whpxlyx')">
					<el-form-item label="危化品泄漏影响" prop="whpxlyx" required >
						<el-input v-model="dataForm.whpxlyx"  :disabled ="JudgeWrite('whpxlyx')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('sbsgyx')">
					<el-form-item label="设备事故影响" prop="sbsgyx" required >
						<el-input v-model="dataForm.sbsgyx"  :disabled ="JudgeWrite('sbsgyx')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('gysgyx')">
					<el-form-item label="工艺事故影响" prop="gysgyx" required >
						<el-input v-model="dataForm.gysgyx"  :disabled ="JudgeWrite('gysgyx')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('zgffcs')">
					<el-form-item label="整改防范措施" prop="zgffcs" required >
						<el-input v-model="dataForm.zgffcs"  :disabled ="JudgeWrite('zgffcs')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('zrrcljg')">
					<el-form-item label="责任人处理结果" prop="zrrcljg" required >
						<el-input v-model="dataForm.zrrcljg"  :disabled ="JudgeWrite('zrrcljg')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('spyj')">
					<el-form-item label="审批意见" prop="spyj" required >
						<el-input v-model="dataForm.spyj"  :disabled ="JudgeWrite('spyj')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
			</el-form>

        </el-row>
        <UserBox v-if="userBoxVisible" ref="userBox" @submit="submit" />
    </div>
</template>
<script>
    import request from '@/utils/request'
    import { previewDataInterface } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                setting: {},
                eventType: '',
                userBoxVisible: false,
                dataForm: {
                    id:'',                    
					flowId: '',
					actionType:1,
					freeApproverUserId: '',
					flowNumber:undefined,
					sgdj:undefined,
					sgfxx:undefined,
					zgzrr:undefined,
					spr:undefined,
					jywcqx:undefined,
					sgzjl:[],
					sgsfxb:[],
					zzdcbg:[],
					mainid:undefined,
					hzssyx:undefined,
					whpxlyx:undefined,
					sbsgyx:undefined,
					gysgyx:undefined,
					zgffcs:undefined,
					zrrcljg:undefined,
					spyj:undefined,

                },
                rules: {
						sgdj:[
						{
							required:true,
							message:'请输入事故等级',
							trigger:'blur'
						},
						],
						sgfxx:[
						{
							required:true,
							message:'请输入事故发现项',
							trigger:'blur'
						},
						],
						zgzrr:[
						{
							required:true,
							message:'请输入整改责任人',
							trigger:'blur'
						},
						],
						spr:[
						{
							required:true,
							message:'请输入审批人',
							trigger:'blur'
						},
						],
						jywcqx:[
						{
							required:true,
							message:'请输入建议完成期限',
							trigger:'blur'
						},
						],
						hzssyx:[
						{
							required:true,
							message:'请输入火灾损失影响',
							trigger:'blur'
						},
						],
						whpxlyx:[
						{
							required:true,
							message:'请输入危化品泄漏影响',
							trigger:'blur'
						},
						],
						sbsgyx:[
						{
							required:true,
							message:'请输入设备事故影响',
							trigger:'blur'
						},
						],
						gysgyx:[
						{
							required:true,
							message:'请输入工艺事故影响',
							trigger:'blur'
						},
						],
						zgffcs:[
						{
							required:true,
							message:'请输入整改防范措施',
							trigger:'blur'
						},
						],
						zrrcljg:[
						{
							required:true,
							message:'请输入责任人处理结果',
							trigger:'blur'
						},
						],
						spyj:[
						{
							required:true,
							message:'请输入审批意见',
							trigger:'blur'
						},
						],

                },
				sgdjOptions:[{"fullName":"特别重大","id":"0"},{"fullName":"重大","id":"1"},{"fullName":"较大","id":"2"},{"fullName":"一般","id":"3"}],
				zgzrrOptions:[],
				sprOptions:[],

					sgzjlList:[],
					sgsfxbList:[],
					zzdcbgList:[],

            }
        },
        computed: {},
        watch: {},
        created() {
		this.getzgzrrOptions();
		this.getsprOptions();

        },
        mounted() { },
        methods: {
		getzgzrrOptions()
			{
				previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
					this.zgzrrOptions = res.data
				})
			},
			getsprOptions()
			{
				previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
					this.sprOptions = res.data
				})
			},

            goBack() {
                this.$emit('refresh')
            },
            init(data) {
                this.loading = true
                this.dataForm.id = data.id || ''
                this.setting = data
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if (this.dataForm.id) {
                        request({
                            url: '/api/WorkFlowForm/qyaqscSgbgxxlc/' + this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataForm = res.data
							this.sgzjlList = this.dataForm.sgzjl ? JSON.parse(this.dataForm.sgzjl) : [];
							this.sgsfxbList = this.dataForm.sgsfxb ? JSON.parse(this.dataForm.sgsfxb) : [];
							this.zzdcbgList = this.dataForm.zzdcbg ? JSON.parse(this.dataForm.zzdcbg) : [];

                        })
                    } else {
                        this.dataForm.flowId = data.flowId
						this.dataForm.mainid = data.mainid; 
                    }
                    this.loading = false
                })
            },
            // 表单提交
            dataFormSubmit(eventType) {            
				this.$set(this.dataForm, 'sgzjl', JSON.stringify(this.sgzjlList));
				this.$set(this.dataForm, 'sgsfxb', JSON.stringify(this.sgsfxbList));
				this.$set(this.dataForm, 'zzdcbg', JSON.stringify(this.zzdcbgList));

                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (eventType === 'audit' || eventType === 'reject') {
                            this.$emit('approval', this.dataForm, eventType)
                            return
                         }
                        this.dataForm.actionType = eventType === 'submit' ? 0 : 1
                        this.eventType = eventType
                        if (this.eventType === 'submit') {
                            if (this.setting.freeApprover == 0) {
                                this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
                                    type: 'warning'
                                }).then(() => {
                                    this.submit()
                                }).catch(() => { });
                            } else {
                                this.userBoxVisible = true
                                this.$nextTick(() => {
                                    this.$refs.userBox.init()
                                })
                            }
                        } else {
                            this.request()
                        }
                    }
                })
            },
            submit(freeApproverUserId) {
                if (freeApproverUserId) this.dataForm.freeApproverUserId = freeApproverUserId
                this.request()
            },
            request() {
                if (!this.dataForm.id) {
                    delete (this.dataForm.id)
                    request({
                        url: '/api/WorkFlowForm/qyaqscSgbgxxlc',
                        method: 'post',
                        data: this.dataForm
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('close', true)
                            }
                        })
                    })
                } else {
                    request({
                        url: '/api/WorkFlowForm/qyaqscSgbgxxlc/' + this.dataForm.id,
                        method: 'PUT',
                        data: this.dataForm
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('close', true)
                            }
                        })
                    })
                }
            },
            JudgeShow(id) {
                if (!this.setting.formOperates || !this.setting.formOperates.length) return true
                let arr = this.setting.formOperates.filter(o => o.id === id) || []
                if (!arr.length) return true
                let item = arr[0]
                return item.read
            },
            JudgeWrite(id) {
                if (!this.setting.formOperates || !this.setting.formOperates.length) return false
                let arr = this.setting.formOperates.filter(o => o.id === id) || []
                if (!arr.length) return true
                let item = arr[0]
                return !item.write
            },

        },
    }

</script>
